package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.KeyPersonnelAdmin;
import com.ruoyi.system.mapper.KeyPersonnelAdminMapper;
import com.ruoyi.system.service.KeyPersonnelAdminService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 两类重点人员(KeyPersonnelAdmin)表服务实现类
 *
 * @author makejava
 * @since 2022-11-18 09:31:40
 */
@Service("keyPersonnelAdminService")
public class KeyPersonnelAdminServiceImpl implements KeyPersonnelAdminService {
    @Resource
    private KeyPersonnelAdminMapper keyPersonnelAdminDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public KeyPersonnelAdmin queryById(Integer id) {
        return this.keyPersonnelAdminDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param keyPersonnelAdmin 筛选条件
     * @param pageRequest       分页对象
     * @return 查询结果
     */
    @Override
    public Page<KeyPersonnelAdmin> queryByPage(KeyPersonnelAdmin keyPersonnelAdmin, PageRequest pageRequest) {
        long total = this.keyPersonnelAdminDao.count(keyPersonnelAdmin);
        return new PageImpl<>(this.keyPersonnelAdminDao.queryAllByLimit(keyPersonnelAdmin, pageRequest), pageRequest, total);
    }

    @Override
    public List<KeyPersonnelAdmin> selectList(KeyPersonnelAdmin keyPersonnelAdmin) {
        return keyPersonnelAdminDao.queryAll(keyPersonnelAdmin);
    }

    @Override
    public KeyPersonnelAdmin selectByName(String name) {
        KeyPersonnelAdmin keyPersonnelAdmin = new KeyPersonnelAdmin();
        keyPersonnelAdmin.setName(name);
        keyPersonnelAdmin.setYn(1);
        List<KeyPersonnelAdmin> list = keyPersonnelAdminDao.queryAll(keyPersonnelAdmin);
        if (StringUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public KeyPersonnelAdmin selectByPositionUserId(Integer positionUserId) {
        KeyPersonnelAdmin keyPersonnelAdmin = new KeyPersonnelAdmin();
        keyPersonnelAdmin.setPositionUserId(positionUserId);
        List<KeyPersonnelAdmin> list = keyPersonnelAdminDao.queryAll(keyPersonnelAdmin);
        if (StringUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 新增数据
     *
     * @param keyPersonnelAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public KeyPersonnelAdmin insert(KeyPersonnelAdmin keyPersonnelAdmin) {
        this.keyPersonnelAdminDao.insert(keyPersonnelAdmin);
        return keyPersonnelAdmin;
    }

    /**
     * 修改数据
     *
     * @param keyPersonnelAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public KeyPersonnelAdmin update(KeyPersonnelAdmin keyPersonnelAdmin) {
        keyPersonnelAdmin.setModifyBy("系统管理员");
        keyPersonnelAdmin.setModifyDate(new Date());
        this.keyPersonnelAdminDao.update(keyPersonnelAdmin);
        return this.queryById(keyPersonnelAdmin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.keyPersonnelAdminDao.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<KeyPersonnelAdmin> keyPersonnelAdmins, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(keyPersonnelAdmins) || keyPersonnelAdmins.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        keyPersonnelAdminDao.insertBatch(keyPersonnelAdmins);
        return "成功";
    }
}
